import request from '@/utils/request'
import qs from 'qs'

// 登录的参数
export interface LoginParams {
  name?: string
  pwd?: string
  phone?: string
  code?: string
  grant_type: 'password' | 'sms_code' | 'social'
  client_id: string
  client_secret: string,
  scope?: string
  response_type?: string
  redirect_uri? : string
}

// 确定授权的请求参数
export interface ConfirmParams{
  response_type?: string
  client_id: string
  redirect_uri? : string
  scope?: string
  build_redirect_uri? : boolean
}

export interface UserInfo {
  id: string
  username: string
  avatar: string
  email: string
  phone: string
}

export const loginApi = (params: LoginParams) => {
  return request.get<{ token: string; user: UserInfo }>('/oauth2/doLogin', {params : params})
}

export const getRedirectUri = (params : LoginParams) =>{
  const queryString = qs.stringify(params)
  return request.post(`/oauth2/getRedirectUri?${queryString}`)
}

export const getTokenByCode = (code : string) =>{
  return request.post(`/platform/getTokenByCode?code=${code}`)
}

export const getAuthorizeInfo = (clientId: string) => {
  return request.get(`/oauth2/authorize?client_id=${clientId}`)
}

export const confirmAuthorize = (params : ConfirmParams) => {
  const queryString = qs.stringify(params)
  console.info(queryString)
  return request.post(`/oauth2/doConfirm?${queryString}`)
}